

package vistas;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import controladores.CCategoria;
import controladores.CProducto;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VProducto extends javax.swing.JFrame {

    private controladores.CProducto control;
    private DefaultTableModel modeloTabla;
    private Vector<String> fila;

	private CProducto producto;
    
  public VProducto(CProducto pro) {
    	super();
		initComponents();
		producto= pro;
        modeloTabla= new DefaultTableModel();
        
        fila= new Vector<String>();
        
        tblIngrediente.setModel(modeloTabla);
        fila.add("Ingrediente");
        fila.add("Cantidad");
        modeloTabla.setColumnIdentifiers(fila);
    }

     public void addListener(ActionListener escuchador){
        btnSalir.addActionListener(escuchador);
        btnRegistrar.addActionListener(escuchador);
        btnCancelar.addActionListener(escuchador);
        btnAgregar.addActionListener(escuchador);
        btnQuitar.addActionListener(escuchador);
        btnSalir.setActionCommand("Salir");
        btnAgregar.setActionCommand("Agregar");
        btnCancelar.setActionCommand("Cancelar");
        btnQuitar.setActionCommand("Quitar");
        btnRegistrar.setActionCommand("Registrar");
    }


    public boolean actualizar(){
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            if(modeloTabla.getValueAt(i, 0).equals(cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex()))){
               modeloTabla.setValueAt(spnCantidad.getValue(), i, 1);
               return true;
            }
        return false;
    }


    public void agregarCategoria(String dato){
        cmbCategoria.setSelectedItem(dato);
    }

    public void agregarFila(String [] v){
        if(!actualizar()){
                fila= new Vector<String>();
                fila.add(v[0]);
                fila.add(v[1]);
                modeloTabla.addRow(fila);
            }
    }

     public void agregarFila1(String [] v){
        fila= new Vector<String>();
        fila.add(v[0]);
        fila.add(v[1]);
        modeloTabla.addRow(fila);
    }

    public void quitar(){
        int i = tblIngrediente.getSelectedRow();
        if(i != -1)
            modeloTabla.removeRow(i);
        else
            JOptionPane.showMessageDialog(new JFrame(),"Debe seleccionar el regitstro que desea quitar","Calle Hambre Arena Plaza",JOptionPane.ERROR_MESSAGE);
    }

     public void reiniciar(){
        txtCodigo.setText("");
        txtDescripcion.setText("");
        txtPrecio.setText("");
        modeloTabla.setRowCount(0);
        cmbCategoria.setSelectedIndex(0);
        cmbIngredientes.setSelectedIndex(0);
        spnCantidad.setValue(0);
     }

    public void cargarComboIngrediente(ArrayList<String> descripciones){
        cmbIngredientes.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbIngredientes.addItem(descripciones.get(i));

        }
    }
    
    public void cargarComboCategoria(ArrayList<String> descripciones){
        cmbCategoria.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbCategoria.addItem(descripciones.get(i));

        }
    }

     public ArrayList<JTextField> getTextos() {
        ArrayList<JTextField> v = new ArrayList<JTextField>();
        v.add(txtCodigo);
        v.add(txtDescripcion);
        v.add(txtPrecio);
        return v;
    }

    public String getTextoCmbIngrediente() {
        return (String) cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex());
    }

    public String getTextoCmbCategoria() {
        return (String) cmbCategoria.getItemAt(cmbCategoria.getSelectedIndex());
    }

    public Integer getCantidad() {
        return (Integer) spnCantidad.getValue();
    }

     public ArrayList<String []> getInfoTabla(){
        ArrayList<String []> v = new ArrayList<String[]>();
        for (int i = 0; i < modeloTabla.getRowCount(); i++){
            String [] s = {""+modeloTabla.getValueAt(i, 0),""+modeloTabla.getValueAt(i, 1)};
            v.add(s);
         }
         return v;
    }

    public String [] getRegistro(){
        String [] s = {""+cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex()),""+spnCantidad.getValue()};

         return s;
    }

    public JTextField getTxtCodigo() {
        return txtCodigo;
    }

    public JTextField getTxtDescripcion() {
        return txtDescripcion;
    }

    public JTextField getTxtPrecio() {
        return txtPrecio;
    }
    


     

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        cmbIngredientes = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        spnCantidad = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        btnAgregar = new javax.swing.JButton();
        btnQuitar = new javax.swing.JButton();
        txtCodigo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDescripcion = new javax.swing.JTextField();
        txtPrecio = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cmbCategoria = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnSalir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnRegistrar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblIngrediente = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Registro de Productos");
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setLayout(null);

        jLabel11.setFont(new java.awt.Font("Ubuntu", 1, 15));
        jLabel11.setText("Ingredientes");
        jPanel1.add(jLabel11);
        jLabel11.setBounds(420, 270, 110, 30);

        jPanel1.add(cmbIngredientes);
        cmbIngredientes.setBounds(30, 70, 170, 30);

        jLabel5.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel5.setText("Selecione");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(30, 30, 170, 30);

        jLabel7.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel7.setText("Cantidad");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(220, 30, 130, 30);
        jPanel1.add(spnCantidad);
        spnCantidad.setBounds(220, 70, 60, 28);

        jLabel4.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel4.setText("Categoria:");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(110, 280, 140, 32);

        jLabel1.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel1.setText("Ingredientes");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(100, 10, 140, 32);

        btnAgregar.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        btnAgregar.setText("Agregar");
        jPanel1.add(btnAgregar);
        btnAgregar.setBounds(30, 120, 120, 40);

        btnQuitar.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        btnQuitar.setText("Quitar");
        jPanel1.add(btnQuitar);
        btnQuitar.setBounds(160, 120, 120, 40);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(80, 180, 310, 180);
        jPanel1.setBackground(new java.awt.Color(168,217,237));
        getContentPane().add(txtCodigo);
        txtCodigo.setBounds(200, 20, 190, 30);

        jLabel3.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel3.setText("Precio:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(80, 100, 224, 32);

        txtDescripcion.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
        getContentPane().add(txtDescripcion);
        txtDescripcion.setBounds(200, 60, 190, 32);

        txtPrecio.setFont(new java.awt.Font("Arial", 0, 13));
        getContentPane().add(txtPrecio);
        txtPrecio.setBounds(200, 100, 190, 30);

        jLabel2.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel2.setText("Descripcion:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(80, 60, 140, 32);

        jLabel6.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel6.setText("Codigo:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(80, 20, 130, 30);

        getContentPane().add(cmbCategoria);
        cmbCategoria.setBounds(200, 140, 190, 30);
        cmbCategoria.setBackground(new java.awt.Color(168,217,237));

        jLabel8.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel8.setText("Categoria:");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(80, 140, 140, 32);

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setLayout(null);

        btnSalir.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        btnSalir.setText("Salir");
        jPanel2.add(btnSalir);
        btnSalir.setBounds(270, 10, 120, 40);

        btnCancelar.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel2.add(btnCancelar);
        btnCancelar.setBounds(140, 10, 120, 40);

        btnRegistrar.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        btnRegistrar.setText("Registrar");
        btnRegistrar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel2.add(btnRegistrar);
        btnRegistrar.setBounds(10, 10, 120, 40);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(40, 560, 400, 60);
        jPanel2.setBackground(new java.awt.Color(168,217,237));

        tblIngrediente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tblIngrediente);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(30, 380, 430, 160);
        jScrollPane2.setBackground(new java.awt.Color(168,217,237));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds(0, 0, 493, 670);
        getContentPane().setBackground(new java.awt.Color(230,238,250));
    }// </editor-fold>//GEN-END:initComponents

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnQuitar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbCategoria;
    private javax.swing.JComboBox cmbIngredientes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner spnCantidad;
    private javax.swing.JTable tblIngrediente;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtPrecio;
    // End of variables declaration//GEN-END:variables



}
